var os = require('os');
var xml2Js = require('xml2js');

exports.getClientId = function(){
	var ifaces = os.networkInterfaces();
	var ip = "";

	Object.keys(ifaces).forEach(function (ifname) {
		  var alias = 0;
		  ifaces[ifname].forEach(function (iface) {
		    if ('IPv4' !== iface.family || iface.internal !== false) {
		      // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
		      return;
		    }

		    if (alias >= 1) {
		      // this single interface has multiple ipv4 addresses
		      //console.log(ifname + ':' + alias, iface.address);
		    } else {
		    	  ip = iface.address;
		      // this interface has only one ipv4 adress
		      //console.log(ifname, iface.address);
		      //break;
		    }
		    //++ alias;
		  });
		});
	return ip;
}

exports.xml2Json = async (xmlStr) => {
  return new Promise(function(resolve,reject){
    try {
      xml2Js.parseString(xmlStr,{explicitArray : false},function(err,json){
        resolve(json);
        //callback(err,json);
      });
    } catch (e) {
      reject(e);
    }
  });
	/*return function (callback){
		xml2Js.parseString(xmlStr,{explicitArray : false},function(err,json){
			callback(err,json);
		});
	}*/
}
